<?php
require(__DIR__."/task.php");
function server($port) {
  echo "Starting server at port $port...\n";

  $socket = @stream_socket_server("tcp://localhost:$port", $errNo, $errStr);
  if (!$socket) throw new Exception($errStr, $errNo);

  stream_set_blocking($socket, 0);

  $socket = new CoSocket($socket);
  while (true) {
      yield newTask(
          handleClient(yield $socket->accept())
      );
  }
}

function handleClient($socket) {
  var_dump($socket);
  $data = (yield $socket->read(8192));

  $msg = "Received following request:\n\n$data";
  $msgLength = strlen($msg);

  $response = <<<RES
HTTP/1.1 200 OK\r
Content-Type: text/plain\r
Content-Length: $msgLength\r
Connection: close\r
\r
$msg
RES;

  yield $socket->write($response);
  yield $socket->close();
}

$scheduler = new Scheduler;
$scheduler->newTask(server(8000));
$scheduler->run();
